package com.pets.springboot_petshop.service.impl;

import com.pets.springboot_petshop.entity.Appointmentslot;
import com.pets.springboot_petshop.mapper.AppointmentslotMapper;
import com.pets.springboot_petshop.service.AppointmentslotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/14 下午3:19 周一
 * @Description: 预约时间和名额实现类
 */
@Service
public class AppointmentslotServiceImpl implements AppointmentslotService {
    @Autowired
    private AppointmentslotMapper appointmentslotMapper;

    // 定时任务，每分钟执行一次
    @Scheduled(fixedRate = 60000)
    public void checkAndUpdateAppointmentSlot() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 查询所有已过期的记录
        List<Appointmentslot> expiredAppointments = appointmentslotMapper.findExpiredAppointments(now);
        // 更新这些记录的状态为不可预约
        for (Appointmentslot appointment : expiredAppointments) {
            appointmentslotMapper.updatestate(appointment.getId(), "不可预约");
        }
    }
    // 定时任务，每分钟执行一次
    @Scheduled(fixedRate = 30000)
    public void Updatestate() {

        //查询所有预约名额为0的记录
        List<Appointmentslot> expiredAppointments = appointmentslotMapper.selectavailable_spotsstate();
        // 更新这些记录的状态为不可预约
        for (Appointmentslot appointment : expiredAppointments) {
            appointmentslotMapper.updatestate(appointment.getId(), "不可预约");
        }
    }


    @Override
    public Appointmentslot selectappointmentslot(int id) {
        return appointmentslotMapper.selectappointmentslot(id);
    }

    @Override
    public List<Appointmentslot> selectname(String name) {
        return appointmentslotMapper.selectname(name);
    }

    @Override
    public List<Appointmentslot> selectOrder(LocalDate date) {
        return appointmentslotMapper.selectOrder(date);
    }


    @Override
    public List<Appointmentslot> findAll() {
        return appointmentslotMapper.findAll();
    }

    @Override
    public int addAppointmentslot(Appointmentslot appointmentslot) {
        return appointmentslotMapper.addAppointmentslot(appointmentslot);
    }

    @Override
    public int deleteAppointmentslotById(int id) {
        return appointmentslotMapper.deleteAppointmentslotById(id);
    }

    @Override
    public int updateAppointmentslotById(Appointmentslot appointmentslot) {
        return appointmentslotMapper.updateAppointmentslotById(appointmentslot);
    }

    @Override
    public List<Appointmentslot> selectservice_id(int service_id) {
        return  appointmentslotMapper.selectservice_id(service_id);
    }

    @Override
    public int batchUpdateStatus(int[] ids, String state) {
        return appointmentslotMapper.batchUpdateStatus(ids, state);
    }

    @Override
    public int admindelete(int[] ids) {
        return appointmentslotMapper.admindelete(ids);
    }
}
